package com.smartens.boit.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.smartens.boit.R;
import com.smartens.boit.adapter.NormalAdapter;
import com.smartens.boit.base.BaseActivity;
import com.smartens.boit.bean.DevicesList;
import com.smartens.boit.util.HttpHelper;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * @Description 正常设备列表
 * @Author songtt
 * @Date 2021/3/4
 */

public class NormalActivity extends BaseActivity {
    private RecyclerView recyclerView;
    private TextView empty;
    private NormalAdapter recycleAdapter;
    private SwipeRefreshLayout swipeRefresh;
    private LinearLayoutManager mLinearLayoutManager;
    private List<DevicesList> mList;
    private ImageView back;
    private HttpHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_normal);
        helper = HttpHelper.getInstance();
        mList = new ArrayList<>();
        getList(1);
        initView1();

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    /**
     * 正常设备列表
     *
     * @param status 设备状态 1：正常   3：离线
     */
    public void getList(int status) {
        helper.GetDevicesList(status, new HttpHelper.HttpCallBack<DevicesList>() {

            @Override
            public void onSuccess(List<DevicesList> result) {
               //  Log.v("获取列表成功111", String.valueOf(result.size()));
                mList = result;
                back.post(new Runnable() {
                    @Override
                    public void run() {
                        recycleAdapter.getData(mList);
                        recycleAdapter.notifyDataSetChanged();
                    }
                });

            }

            @SuppressLint("LongLogTag")
            @Override
            public void onFailure(final String message) {
            //     Log.v("获取列表失败", message);
            }
        });

    }


    public void initView1() {

//        mList = new ArrayList();
        back = findViewById(R.id.tv_back);
        empty = findViewById(R.id.empty);
        swipeRefresh = findViewById(R.id.swipe_refresh);
        recyclerView = findViewById(R.id.recycler);
        if (mList.size() > 0) {
            empty.setVisibility(View.VISIBLE);
        } else {
            empty.setVisibility(View.GONE);
        }
        mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

        recycleAdapter = new NormalAdapter(mList);
        //设置布局管理器
        recyclerView.setLayoutManager(mLinearLayoutManager);
        //设置Adapter
        recyclerView.setAdapter(recycleAdapter);

        recycleAdapter.setOnItemClickListener(new NormalAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getApplicationContext(), EquipmentInfoActivity.class);
                intent.putExtra("mac",mList.get(position-1).getGatewayMac());
                startActivity(intent);
            }
        });
        recycleAdapter.setOnControlClickListener(new NormalAdapter.OnControlClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getApplicationContext(), ControlActivity.class);
                startActivity(intent);
            }
        });
        recycleAdapter.setOnEnergyClickListener(new NormalAdapter.OnEnergyClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getApplicationContext(), EquipmentEnergyActivity.class);
                startActivity(intent);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NormalActivity.this.finish();
            }
        });
        handleDownPullUpdate();
    }
    private void handleDownPullUpdate() {
        swipeRefresh.setEnabled(true);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getList(1);
                //     Log.v("下拉刷新成功", "下拉刷新成功");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefresh.setRefreshing(false);
                    }
                }, 1500);
                Toast toast=Toast.makeText(NormalActivity.this,"刷新成功!",Toast.LENGTH_SHORT);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.show();
            }
        });
    }

}